package com.chat.demo.userservice.Entity.Entity;

import com.chat.demo.userservice.DTO.User.CouldUpdateUserInfo;
import com.chat.demo.userservice.DTO.User.RegisterRequest;
import com.chat.demo.userservice.DTO.User.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.Date;

@Mapper
public interface UserDao {
    @Select("SELECT * FROM app.user WHERE user_id = #{userId}")
    @Results(id = "userResult", value = {@Result(property = "userId", column = "user_id"), @Result(property = "username", column = "username"), @Result(property = "email", column = "email"), @Result(property = "password", column = "password"), @Result(property = "createdAt", column = "created_at"), @Result(property = "lastLogin", column = "last_login")})
    User findUserById(@Param("userId") Long userId);

    @Insert("INSERT INTO app.user (username, email, password) VALUES (#{username}, #{email}, #{password})")
    void register(RegisterRequest user);

    @Select("SELECT app.user.last_login FROM app.user WHERE user_id = #{userId}")
    Date findLastLoginTime(@Param("userId") Long userId);

    @Select("SELECT app.user.created_at FROM app.user WHERE user_id = #{userId}")
    Date findCreateTime(@Param("userId") Long userId);

    @Select("SELECT app.user.email FROM app.user WHERE user_id = #{userId}")
    String findEmail(@Param("userId") Long userId);

    @Select("SELECT user_id,username,email,created_at,last_login FROM app.user WHERE user_id = #{userId}")
    @ResultType(UserInfo.class)
    UserInfo findUserInfoByID(@Param("userId") Long userId);

    @Select("SELECT online FROM app.user WHERE user_id = #{userId}")
    boolean findUserOnlineStatusByUserID(@Param("userId") Long userId);

    @Update("UPDATE app.user SET online = #{online} WHERE user_id = #{userId}")
    void updateOnlineStatus(@Param("userId") Long userId, @Param("online") boolean online);

    @Update("UPDATE app.user SET username = #{userInfo.username}, email = #{userInfo.email} WHERE user_id = #{userId}")
    int updateUserInfo(@Param("userInfo") CouldUpdateUserInfo userInfo, @Param("userId") Long userId);

    @Update("UPDATE app.user SET password = #{newPassword} WHERE user_id = #{userId}")
    void updateUserPassword(@Param("newPassword") String newPassword, @Param("userId") Long userId);

    @Update("UPDATE app.user SET online = false WHERE user_id = #{userId}")
    void logout(@Param("userId") Long userId);

    @Select("SELECT * FROM app.user WHERE username = #{username}")
    User findUserByUsername(String username);

    @Update("UPDATE app.user SET last_login = NOW() WHERE user_id = #{userId}")
    void updateLastLogin(@Param("userId") Long userId);
}